Imports ccapps.persistent.billcycleUpdate
Namespace Web.page

    Public Class BillcycleUpdate
        Inherits [lib].BasePage

        Protected WithEvents txt_custid As System.Web.UI.WebControls.TextBox
        Protected WithEvents DG_CUST As System.Web.UI.WebControls.DataGrid
        Protected WithEvents Lblok As System.Web.UI.WebControls.Label
        Protected WithEvents dg_contracts As System.Web.UI.WebControls.DataGrid
        Protected WithEvents proc As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents LBL_CO As System.Web.UI.WebControls.Label

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Me.PageTitle = "Billcycle Update"

            Dim Vproc As String
            Vproc = Me.proc.Value
            Me.proc.Value = ""

            Select Case Vproc
                Case "search"
                    search()
            End Select


        End Sub

        Public Sub BindGrid()
            Dim sql As String
            Dim temp As New DataTable
            temp = getGridData(Me.txt_custid.Text)

            'sql = "select cus.customer_id c1,cus.billcycle BID, cus.billcycle||' '||b.description BCYCLE,m.des CUSTMCODE "
            'sql = sql & " from customer_all cus, billcycles b,mputmtab m where cus.billcycle=b.billcycle "
            'sql = sql & "  and cus.tmcode=m.tmcode and m.vscode=0 and cus.customer_id= " & Me.txt_custid.Text & "  "

            If temp.Rows.Count > 0 Then
                Me.DG_CUST.Visible = True
                Me.DG_CUST.DataSource = temp
                Me.DG_CUST.DataBind()
                Me.Lblok.Text = ""
            Else
                Me.Lblok.Text = " No data found "
                Me.DG_CUST.Visible = False
            End If


        End Sub

        Public Sub BindGridCo()
            'Dim sql As String
            'Dim ds As New myDBConn

            Dim temp As New DataTable
            temp = getCoGridData(Me.txt_custid.Text)

            'sql = " SELECT co.co_id COID,dn.dn_num MSISDN,m.des tmcode,ch.ch_validfrom ACT_DATE,CUR.ddate DEACT_DATE  "
            'sql = sql & " FROM "
            'sql = sql & " contr_services_cap cap,eu_bill.view_directory_number dn,contract_history ch,contract_all co,MPUTMTAB M,  "
            'sql = sql & " (SELECT curr.co_id,curr.ch_validfrom ddate FROM contract_history curr WHERE ch_status='d')cur  "
            'sql = sql & " WHERE cap.co_id=co.co_id  "
            'sql = sql & " AND cap.dn_id=dn.dn_id  "
            'sql = sql & " AND co.co_id=ch.co_id"
            'sql = sql & " AND ch.co_id=cur.co_id(+)"
            'sql = sql & " AND CO.TMCODE=M.TMCODE"
            'sql = sql & " AND CAP.SEQNO=(SELECT MAX(CAP1.SEQNO) FROM CONTR_SERVICES_CAP CAP1 WHERE CAP.CO_ID=CAP1.CO_ID)"
            'sql = sql & " AND CH.CH_SEQNO=2"
            'sql = sql & " AND cap.co_id>1"
            'sql = sql & " AND dn.dn_id>1"
            'sql = sql & " AND m.vscode=0"
            'sql = sql & " AND Co.customer_ID='" & Me.txt_custid.Text & "'"


            If temp.Rows.Count > 0 Then
                Me.dg_contracts.Visible = True
                Me.dg_contracts.DataSource = temp
                Me.dg_contracts.DataBind()
                Me.LBL_CO.Visible = True
            Else
                Me.LBL_CO.Visible = False
                Me.dg_contracts.Visible = False
            End If
        End Sub

        Public Function is_number(ByVal var As String) As Boolean
            Dim i As Integer
            If Len(var) = 0 Then Return False
            For i = 0 To Len(var) - 1
                If Asc(var.Chars(i)) < 48 Or Asc(var.Chars(i)) > 57 Then
                    Return False
                    Exit Function
                End If
            Next

            Return True
        End Function

        Private Sub search()
            If is_number(Me.txt_custid.Text) Then

                DG_CUST.EditItemIndex = -1
                BindGrid()
                BindGridCo()
            Else
                Me.Lblok.Text = "Please enter numeric value"
            End If
            '  DG_CUST.EditItemIndex = -1

        End Sub

        Protected Sub dgr_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

            Dim i As Integer = e.Item.ItemIndex
            ' start the editing mode
            DG_CUST.EditItemIndex = CType(e.Item.ItemIndex, Integer)
            BindGrid()

        End Sub

        Protected Sub dgr_CancelEdit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
            ' stop the editing mode
            DG_CUST.EditItemIndex = -1
            BindGrid()

        End Sub

        Public Sub dgr_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
            Dim ds As New myDBConn
            Dim billcycle_value As String = CType(e.Item.FindControl("dd_bcycle"), DropDownList).SelectedItem.Value
            Dim NEW_billcycle As String = CType(e.Item.FindControl("dd_bcycle"), DropDownList).SelectedItem.Text

            'Dim os_user As String = Request.ServerVariables("LOGON_USER")
            Dim os_user As String = Session("user")
            Dim cust_id As Long = CType(DG_CUST.DataKeys(e.Item.ItemIndex), Long)
            'Dim sql As String = "select cus.billcycle||' '||b.description billcycle "
            'sql = sql & " from customer_all cus,billcycles b where cus.billcycle=b.billcycle and cus.customer_id=" & id & ""

            Dim old_billcycle As String = getOldBillcycle(Me.txt_custid.Text).Rows(0)(0)
            'Response.Write(old_billcycle)
            'Response.End()

            Try
                'sql = "update customer_all set billcycle ='" & billcycle_value & "' where customer_id =" & id & ""
                updateBillcycle(cust_id, billcycle_value)
                'ds.ORAExecute(sql)
                Me.Lblok.Text = "Billcycle Successfully Updated"
            Catch myError As Exception
                Me.Lblok.Text = "Database Error: " & myError.ToString()
            End Try

            'sql = "insert into bcupdlog (custid,obillcycle,nbillcycle,usr) values(" & id & ",'" & old_billcycle & "','" & NEW_billcycle & "','" & os_user & "');"
            'ds.Execute(sql)
            insertBillcycleLog(cust_id, old_billcycle, NEW_billcycle, os_user)
            DG_CUST.EditItemIndex = -1
            BindGrid()
        End Sub



        Public Sub DataGrid_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)

            Dim sql As String
            Dim ds As New myDBConn

            sql = "select billcycle BID, billcycle||' '||description bcycle from billcycles"
            'Determine the ListItem Type
            Dim itemType As ListItemType = e.Item.ItemType

            'We only want to run this code for items in edit mode
            If (itemType = ListItemType.EditItem) Then
                'Locate our DropDownList
                Dim myList As DropDownList = CType(e.Item.FindControl("dd_bcycle"), DropDownList)

                'Grab the current row of data - we need that so we know what value to select in the ddl
                Dim myRow As DataRowView = CType(e.Item.DataItem, DataRowView)

                'Grab the value we want to select from the current row of data in our DataGrid/List/Repeater
                Dim mybcycle As String = myRow("bcycle").ToString()

                'Fill our DropDownList with the standard list of data
                myList.DataSource = ds.myORATable(sql)
                'Set the Text and Value field for our DropDownList
                myList.DataTextField = "bcycle"
                myList.DataValueField = "BID"
                myList.DataBind()

                'Use the FindByValue function to locate the item we want to select
                'There is also a FindByText function that can be used in the same way
                myList.Items.FindByText(mybcycle).Selected = True
            End If
        End Sub



        Private Sub DG_CUST_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DG_CUST.SelectedIndexChanged

        End Sub
    End Class
End Namespace